﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

using FRL.DebtChased.Repository.ViewModels;
using FRL.DebtChased.WebServices;

namespace FRL.DebtChased.Repository
{

	/// <summary>
	/// Extensions to the AccountInvoiceStatus model
	/// </summary>
	public partial class AccountInvoiceStatus
	{

		/////////////////////////////////////////////////////////////
		// Static operations

		public static Dictionary<int,string> GetDictionary(DebtChasedEntities ent, bool bAddNoneOption)
		{

			// Load the users
			var statusList = from ais in ent.AccountInvoiceStatus
							 orderby ais.Name
							 select new { ais.AccountInvoiceStatusId, ais.Name };

			// Declare return variable
			Dictionary<int,string> Ftmp = null;
			if (bAddNoneOption)
			{
				Ftmp = new Dictionary<int, string>();
				Ftmp.Add(0, Definitions.None);
				foreach (var ob in statusList)
					Ftmp.Add(ob.AccountInvoiceStatusId, ob.Name);
			}
			else
				Ftmp = statusList.ToDictionary(ais => ais.AccountInvoiceStatusId, ais => ais.Name);

			// Return variable
			return Ftmp;

		}

		public static Dictionary<int,string> GetInstalmentDictionary(DebtChasedEntities ent, bool bAddNoneOption)
		{

			// Load the users
			var statusList = from ais in ent.AccountInvoiceStatus
							 where (ais.IsInstalmentPlanStatus)
							 orderby ais.Name
							 select new { ais.AccountInvoiceStatusId, ais.Name };

			// Declare return variable
			Dictionary<int,string> Ftmp = null;
			if (bAddNoneOption)
			{
				Ftmp = new Dictionary<int, string>();
				Ftmp.Add(0, Definitions.None);
				foreach (var ob in statusList)
					Ftmp.Add(ob.AccountInvoiceStatusId, ob.Name);
			}
			else
				Ftmp = statusList.ToDictionary(ais => ais.AccountInvoiceStatusId, ais => ais.Name);

			// Return variable
			return Ftmp;

		}

		public static string GetName(DebtChasedEntities ent, int iStatusId)
		{

			// Look up status name
			return (from ais in ent.AccountInvoiceStatus
					where (ais.AccountInvoiceStatusId == iStatusId)
					select ais.Name).FirstOrDefault();

		}

		public static string SetNew( DebtChasedEntities ent, ref int iInvoiceStatusId, string strInvoiceStatusName, string strDefaultWorkflowName, bool bIsInstalmentPlanStatus, bool bIsInQuery, int iUserId )
		{
#if DEBUG
			Debug.Assert(iInvoiceStatusId == 0);
#endif // #if DEBUG

			// Declare return variable
			string strError = null;

			// Reset function parameters
			iInvoiceStatusId = 0;

			try
			{

				// Add an account invoice status
				AccountInvoiceStatus ais = AccountInvoiceStatus.CreateAccountInvoiceStatus(
					0,
					strInvoiceStatusName,
					true,
					bIsInstalmentPlanStatus, 
					bIsInQuery
				);
				ent.AccountInvoiceStatus.AddObject(ais);				

				// Save changes			
				ent.SaveChanges();

				//  Update function parameter
				iInvoiceStatusId = ais.AccountInvoiceStatusId;

				// Create an empty workflow
				FRL.DebtChased.WorkflowObjects.Workflow wf = new WorkflowObjects.Workflow();
				wf.Name = strDefaultWorkflowName;
				string strXml = Encoding.Unicode.GetString(wf.ToXmlBuffer());

				// Create a new workflow record
				Workflow newWorkflow = Workflow.CreateWorkflow(0, strDefaultWorkflowName, strXml, true, iInvoiceStatusId);
				ent.Workflows.AddObject(newWorkflow);

				// Save changes			
				ent.SaveChanges();

				// Update the invoice-status default workflow
				ais.DefaultWorkflowId = newWorkflow.WorkflowId;

				// Save changes
				ent.SaveChanges();

			}
			catch (Exception x)
			{

				// Process exception
				strError = ErrorProcessing.HandleException(x, "create invoice status", iUserId);

			}

			// Return variable
			return strError;

		}

		public static string UpdateExisting( DebtChasedEntities ent, int iInvoiceStatusId, string strInvoiceStatusName, bool bIsInstalmentPlanStatus, bool bIsInQuery, int iUserId )
		{
#if DEBUG
			Debug.Assert(iInvoiceStatusId != 0);
#endif // #if DEBUG

			// Declare return variable
			string strError = null;

			try
			{

				// Load the status entity object
				AccountInvoiceStatus status = (from ais in ent.AccountInvoiceStatus
												where (ais.AccountInvoiceStatusId == iInvoiceStatusId)
												select ais).FirstOrDefault();
				if (status != null)
				{

					// Update status
					status.Name = strInvoiceStatusName;
					status.IsInstalmentPlanStatus = bIsInstalmentPlanStatus;
					status.IsInQuery = bIsInQuery;

					// Save changes			
					ent.SaveChanges();

				}
				else
					strError = string.Format("Invoice status[{0}] entity not found in system.",iInvoiceStatusId);

			}
			catch (Exception x)
			{

				// Process exception
				strError = ErrorProcessing.HandleException(x, "update invoice status", iUserId);

			}

			// Return variable
			return strError;

		}

		public static int? GetWorkflowId(DebtChasedEntities ent, int iAccountInvoiceStatusId, int iClientId)
		{

			// TODO
			throw new NotImplementedException();
			return null;

		}

		public static void GetNIAccountInvoiceStatus( DebtChasedEntities ent, out int iNIAccountInvoiceStatusId, out string strNIAccountInvoiceStatusName )
		{

			// Get the invoice status for new instructions

			// Load the system-config table
			iNIAccountInvoiceStatusId = (from sc in ent.SystemConfigs select sc.NIAccountInvoiceStatusId).First();

			// Get the status name
			int iTemp = iNIAccountInvoiceStatusId;
			strNIAccountInvoiceStatusName = (from ais in ent.AccountInvoiceStatus where ais.AccountInvoiceStatusId==iTemp select ais.Name).First();

		}

		public static AccountInvoiceStatusTransport GetTransport( DebtChasedEntities ent, int iInvoiceStatusId )
		{

			// Declare return variable
			AccountInvoiceStatusTransport Ftmp = null;

			// Load account status
			AccountInvoiceStatus status = (from ais in ent.AccountInvoiceStatus
										   where ais.AccountInvoiceStatusId == iInvoiceStatusId
										   select ais).FirstOrDefault();
			if (status != null)
				Ftmp = new AccountInvoiceStatusTransport()
				{
					AccountInvoiceStatusId = status.AccountInvoiceStatusId,
					InvoiceStatusName = status.Name,
					ClientWorkflowId = 0,
					DefaultWorkflowId = status.DefaultWorkflowId ?? 0,
					InUse = false,
					HasGlobalScope = status.IsGlobalScope,
					IsInstalmentPlanStatus = status.IsInstalmentPlanStatus,
					IsInQuery = status.IsInQuery
				};
#if DEBUG
			else
				Debug.Assert(false);
#endif // #if DEBUG

			// Return variable
			return Ftmp;

		}

	}

}
